package com.bafa.front.utils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bafa.debug.Debug;
import com.bafa.exceptions.UserDoesNotExistException;
import com.bafa.front.presentationbeans.SeasonBean;
import com.bafa.front.presentationbeans.TeamBean;
import com.bafa.objects.AdminUser;
import com.bafa.objects.User;
import com.bafa.objects.Team;
import com.bafa.objects.season.Season;
import com.bafa.services.LogonServices;
import com.bafa.services.RetrieveServices;

public class FrontUtils {

	
	public static boolean isLoggedOn(HttpServletRequest request) {

		Debug.debug(new FrontUtils().getClass(), "isLoggedOn",
				"Checking to see if we are logged on");
		if ((request.getSession().getAttribute("LOGGEDON") == null)) {
			Cookie[] cookies = request.getCookies();

			boolean foundCookie = false;
			if (cookies != null) {
				
				for (int i = 0; i < cookies.length; i++) {
					Debug.debug(new FrontUtils().getClass(), "isLoggedOn",
					"This is cookie number "+i);
					Cookie c = cookies[i];

					if (c.getName().equals("bafaloggedon")) {
						Debug.debug(new FrontUtils().getClass(), "isLoggedOn",
								"We have found bafaloggedon");
						try {
							User user = LogonServices.logon(c.getValue());
							Debug.debug(new FrontUtils().getClass(), "isLoggedOn",
							"We've tried to logon");
							if (user != null
									&& user.getRights().equalsIgnoreCase(
											"admin")) {
								AdminUser admin = new AdminUser(null, user
										.getRights(), user.getUserId());
								request.getSession().setAttribute("LOGGEDON",
										admin);
							} else if (user != null) {
								request.getSession().setAttribute("LOGGEDON",
										user);
							}
						} catch (UserDoesNotExistException user) {
							user.printStackTrace();
						}
						foundCookie = true;
					}
				}
			}
		}
		return (request.getSession().getAttribute("LOGGEDON") != null);
	}
	
	public static void rememberMeCookie(HttpServletResponse response, String user){
		Cookie cookie = new Cookie("bafaloggedon",user);
		cookie.setMaxAge(2592000);
		response.addCookie(cookie);
	}
	public static boolean isLoggedOnAsAdmin(HttpServletRequest request){
		return (request.getSession().getAttribute("LOGGEDON")!=null && request.getSession().getAttribute("LOGGEDON") instanceof AdminUser);
	}
	
	public static User getUser(HttpServletRequest request){
		return (User)request.getSession().getAttribute("LOGGEDON");
	}
	
	public static SeasonBean getThisSeason(HttpServletRequest request){
		if(request.getSession().getAttribute("THISSEASON")!=null){
			return (SeasonBean)request.getSession().getAttribute("THISSEASON");
		}
		return new SeasonBean(RetrieveServices.getCurrentSeason());
	}
	
	public static TeamBean getLoggedOnTeam(HttpServletRequest request){
		return (TeamBean)request.getSession().getAttribute("LOGGEDONTEAM");
	}
}
